Practical-13
Mobile Application
Practical List
Create an android application to pick any image from the native application gallery and display it on the screen.
Steps
- Create a new Android project in Android Studio.
- Open the
activity_main.xmllayout file. - Add an
ImageViewelement to the layout with the following attributes:android:id="@+id/imageView"android:layout_width="match_parent"android:layout_height="match_parent"
- Open the
MainActivity.javafile. - Inside the
onCreatemethod, add the following code to set the content view to theactivity_main.xmllayout:setContentView(R.layout.activity_main); - Create a new method called
pickImageto open the image picker:private void pickImage() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
} - Override the
onActivityResultmethod to handle the result of the image picker:@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
Uri imageUri = data.getData();
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageURI(imageUri);
}
} - Add the necessary permissions to the
AndroidManifest.xmlfile:<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> - Run the application on an emulator or a physical device.
- Click the "Pick Image" button to open the image picker.
- Select an image from the gallery.
- The selected image will be displayed on the screen.